/**
 * 
 */
package com.zc.BankCreditManagement.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.zc.BankCreditManagement.DAO.CustomerCreditLoanDAO;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevel;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLoan;
import com.zc.BankCreditManagement.Service.CustomerCreditLoanService;
import com.zc.BankCreditManagement.ZCUtil.GenerateIdUtil;


/**
 * @author ZhangCheng
 * @time 2019年9月23日
 */
public class CustomerCreditLoanServiceImpl implements CustomerCreditLoanService {
	CustomerCreditLoanDAO cCLoanDAO;

	public CustomerCreditLoanServiceImpl() {
		this.cCLoanDAO = new CustomerCreditLoanDAO();
	}

	public int insertNewToDB(CustomerCreditLoan cusCLoan) {

		return this.cCLoanDAO.insertNewCreLoan(cusCLoan);
	}

	/**
	 * 判断是否有进行的贷款
	 * 
	 */
	public boolean isLoaningById(int cusId) {
		if (getLoaningById(cusId) == null) {// 若可以查到当前没有贷款则返回可贷款
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取当前正在进行的贷款
	 * 
	 */

	public CustomerCreditLoan getLoaningById(int cusId) {
		return this.cCLoanDAO.getCreditLoaningById(cusId);
	}

	/**
	 * 置isdelete为1代表磁记录已被删除，表示贷款已还清
	 * 
	 */

	public int finishThisLoan(int cusId) {
		if (this.cCLoanDAO.getCreditLoaningById(cusId) != null) {
			return this.cCLoanDAO.deleteCusLoanById(cusId);
		} else {
			System.out.println("更新失败！您的客户信息有误，不存在该用户或已经被删除，请检查您的信息！");
		}
		return 0;
	}

	/**
	 * 获取所有正在进行的贷款
	 * 
	 */
	public ArrayList<CustomerCreditLoan> getAllLoans(int cusId) {
		return this.cCLoanDAO.getCreditLoansById(cusId, false);
	}

	/**
	 * 信用贷 根据用户Id和用户输入的期望值进行信用贷款以及可贷型判断
	 * 
	 * @return 字符串 null have "空 有正在进行的贷款" null null "空 插入失败" "" + cusWantNum
	 *         "客户所需的期望贷款值"
	 * @throws ParseException 
	 */
	public String cusCreditLoaningByCusId(int cusId, int cusWantNum) throws ParseException {

		if (isLoaningById(cusId)) {// 返回true代表有正在进行的贷款 则不可进行继续贷款操作
			return "null have";
		} else {// false则代表没有正在进行的贷款可以进行继续操作
		
//			System.out.println("cusWantNum" + cusWantNum); 
			Date beginDate = new SimpleDateFormat().parse(new SimpleDateFormat("YYYY-MM-dd").format(new Date())) ;
			CustomerCreditLoan cusCLoan = new CustomerCreditLoan(cusId, GenerateIdUtil.generateCusCreditLoanId(cusId), cusWantNum, beginDate);
			if (insertNewToDB(cusCLoan) == 1) {
				return "" + cusWantNum;
			} else {
				return "null null";
			}
		}
	}

	/**
	 * 根据用户Id获取客户信用等级类型
	 * 
	 * @return null null 信用等级不够 质押物贷款和信用贷款均不允许 null Deposit 信用等级仅可以进行质押物贷款 不足以进行信用贷款
	 *         Level + Money 信用贷款和质押物贷款均可以实现 Money为当前信用等级可以最高信用贷额度
	 */
	public String getCusLoanType(int cusId) {
		CustomerCreditLevel cCL = new CustomerCreditLevelServiceImpl().getcCLById(cusId, true);
		int level = 0;
		if(cCL != null) {
			level = cCL.getCreditLevel();
		}
		
		String msg = "null null";
		if (level >= 3 && level < 5) {
			msg = "null Deposit";
		} else if (level >= 5 && level < 10) {
			msg = "Level 5000";
		} else if (level >= 10 && level < 15) {
			msg = "Level 10000";
		} else if (level >= 15 && level < 25) {
			msg = "Level 20000";
		} else if (level >= 25) {
			msg = "Level 50000";
		}
		return msg;
	}

	/**
	 * 组合贷 传入相对应返回的值进行拼接以传输给前端进行判断
	 * 
	 * @return Deposit null 质押物贷款失败 （达质押物数量上限） Deposit + Man 贷款成功 Man为被分配的经手经理的Id
	 *         Credit null 信用贷款失败（有正在进行的贷款） Credit + Num 信用贷款成功 Num为申请成功的金额
	 */
	public String combineCreditAndDeposit(String deposit, String credit) {
		StringBuffer msg = new StringBuffer();
		if ("null".equals(deposit)) {
			msg.append("Deposit null");
		} else {
			msg.append("Deposit " + deposit);
		}
		if ("null".equals(credit.split(" ")[0])) {
			msg.append("+Credit null");
		} else {
			msg.append("+Credit " + credit);
		}
		return msg.toString();
	}

}
